// JavaScript Document
;(function($){
	$.extend($.fn,{
		zTreeComboBox:function(options){
			var options =$.extend({},$.fn.zTreeComboBox.defaults,options);
			options.zNodes =$.map(options.zNodes, function(obj){
				return $.extend(true,{},obj);//返回对象的深拷贝
			});
			
			var $this = $(this);
			var this_id = $this.attr("id");
			
			var check= {
				enable: true,
				chkboxType: {"Y":"", "N":""}
			};
			if(options.radio){
				check= {
					enable: true,
					chkStyle: "radio",
					radioType: "all"
				};
			}
			
			var setting = {
				check: check,
				view: {
					dblClickExpand: false
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					beforeClick: beforeClick,
					onCheck: onCheck
				}
			};
            //添加树下拉的按钮
			var menuBtn ="menuBtn_"+this_id;  
			$this.after(options.appendBtn);
			$this.next("span").attr({"id":menuBtn});//为按钮添加id
			var $menuBtn=$("#"+menuBtn); 

			//添加下拉树模块start
			var menuContent ="menuContent_" + this_id; //包裹树的div框的id
			var tree = "tree_"+ this_id;//树的id
			var treeHtml='<div id="'+menuContent+'" class="zt_menuContent" style="display:none; position: absolute;">' +
                			'<ul id="'+tree+'" class="ztree" style="margin-top:0;"></ul>' +
            			'</div>'
			$(this).parent().append(treeHtml);
			var $menuContent = $("#"+menuContent);
			var $tree = $("#"+tree);
			
			var tree_width=$this.outerWidth()+"px";
			if(options.width){
				tree_width=options.width;
			}
			var tree_height =options.height;
			//设置树的宽高
			$menuContent.css({"width":tree_width,"height":tree_height});
			//添加下拉树模块end
			if(options.url){
				$.ajax({
					url:options.url,
					type:"post",
					data:options.data,
					dataType:"json",
					success:function(result){
						options.zNodes =result;
						var checkIds=$("#"+options.inputHideId).val().split(",");
						var checkNames ="";
						for(var i in options.zNodes){
							var id=options.zNodes[i]["id"];
							if(jQuery.inArray(id, checkIds)!=-1 ){
								options.zNodes[i].checked=true;
								checkNames += options.zNodes[i].name +",";
							}
						}
						if(checkNames) checkNames=checkNames.substring(0, checkNames.length-1);
						$this.attr({"value":checkNames,"title":checkNames});
					}
				});
			}
			
			
			var treeObj=null;
			//输入框点击时显示树结构
			if($this.attr("noselect")){
				
			}else{
				$this.on("click",showMenu);//添加点击事件
				$menuBtn.on("click",showMenu);//添加点击事件
			}
			function showMenu(){
				if(!treeObj){
					initCheck();
					treeObj = $.fn.zTree.init($tree, setting, options.zNodes);
					/*
					if(options.url){
						$.ajax({
							url:options.url,
							type:"post",
							data:options.data,
							success:function(result){
								options.zNodes =JSON.parse(result);
								initCheck();
								treeObj = $.fn.zTree.init($tree, setting, options.zNodes);
							}
						});
					}else{
						initCheck();
						treeObj = $.fn.zTree.init($tree, setting, options.zNodes);
					}
					*/
				}
				
				var $thisOffset = $this.offset();
				$menuContent.css({left:"0px", top:$this.outerHeight() + "px"}).slideDown("fast");
				$("html").bind("mousedown", onBodyDown);	
			}
			
			//树节点点击前触发
			function beforeClick(treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj(tree);
				zTree.checkNode(treeNode, !treeNode.checked, null, true);
				return false;
			}
			
			//树节点点击时触发
			function onCheck(e, treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj(tree),
				nodes = zTree.getCheckedNodes(true),
				t_name = "";
				t_id="";
				for (var i=0, l=nodes.length; i<l; i++) {
					t_name += nodes[i].name + ",";
					t_id += nodes[i].id +",";
				}
				if (t_id.length > 0 ){
					t_name = t_name.substring(0, t_name.length-1);
					t_id = t_id.substring(0, t_id.length-1);
				}
				
				$this.attr({"value":t_name,"title":t_name});
				$("#"+options.inputHideId).attr("value",t_id);
				if(options.onCheck){
					options.onCheck(treeNode);
				}
			}
	
			function hideMenu() {
				$menuContent.fadeOut("fast");
				$("html").unbind("mousedown", onBodyDown);
			}
			function onBodyDown(event) {
				if (!(event.target.id == menuBtn || event.target.id == this_id || event.target.id == menuContent
						|| $(event.target).parents("#"+menuContent).length>0)) {
					hideMenu();
				}
			}
			
			//初始化勾选start
			function initCheck(){
				var checkIds=$("#"+options.inputHideId).val().split(",");
				var checkNames ="";
				for(var i in options.zNodes){
					var id=options.zNodes[i]["id"];
					if(id) id=id.toString();
					if(jQuery.inArray(id, checkIds)!=-1 ){
						options.zNodes[i].checked=true;
						checkNames += options.zNodes[i].name +",";
					}
				}
				if(checkNames) checkNames=checkNames.substring(0, checkNames.length-1);
				$this.attr({"value":checkNames,"title":checkNames});
			}
		}  
	});	
	
	$.fn.zTreeComboBox.defaults={
		inputHideId:"",//隐藏输入框的id
		appendBtn:'<span class="input-group-addon"><i class="ace-icon fa fa-user"></i> </span>',
		radio:false	,
		width :"" ,//下拉框的宽度
		height:"200px", //下拉框的高度
		zNodes:[], //json数据 (如果设置了url则默认从url上访问数据)
		url :"",   // 数据的url
		data:{},
		onCheck : null
	}
})(jQuery);